package top.yongyan0624.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import top.yongyan0624.pojo.Clazz;
import top.yongyan0624.pojo.Student;
import top.yongyan0624.pojo.Teacher;
import top.yongyan0624.service.ClazzService;
import top.yongyan0624.service.TeacherService;

import java.util.List;

/**
 * @author Yongyan
 * @description: TODO
 * @date 2020/12/23
 **/

@Controller
@RequestMapping("/clazz")
@SessionAttributes("error")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/toAddClazzPage")
    public String toAddClazzPage(Model model){
        List<Teacher> teacherList = teacherService.findAllTeacher();
        model.addAttribute("teacherList",teacherList);
        model.addAttribute("error","");

        return "addClazzPage";
    }

    @RequestMapping("/addClazz")
    public String addClazz(Clazz clazz, Model model){
        Clazz c = clazzService.findById(clazz.getCno());
        if (c == null){
            clazzService.addClazz(clazz);
        } else {
            model.addAttribute("error","该班级编号已被录入！");
            return "addClazzPage";
        }
        return "redirect:/clazz/toFindAllClazzPage/1/5";
    }

    @RequestMapping("/deleteClazz/{id}")
    public String deleteClazz(@PathVariable("id") Integer id){
        clazzService.deleteClazz(id);
        return "redirect:/clazz/toFindAllClazzPage/1/5";
    }

    @RequestMapping("/toUpdateClazzPage/{id}")
    public String toUpdateClazzPage(@PathVariable Integer id, Model model){
        Clazz clazz = clazzService.findById(id);
        model.addAttribute("clazz",clazz);
        List<Teacher> teacherList = teacherService.findAllTeacher();
        model.addAttribute("teacherList",teacherList);

        return "updateClazzPage";
    }

    @RequestMapping("/updateClazz")
    public String updateClazz(Clazz clazz){
        clazzService.updateClazz(clazz);

        return "redirect:/clazz/toFindAllClazzPage/1/5";
    }

    @RequestMapping("/toFindAllClazzPage/{pageNum}/{pageSize}")
    public String toFindAllPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize,Model model){
        PageInfo<Clazz> pageInfo = clazzService.findAllClazz(pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("clazzList",pageInfo.getList());

        List<Teacher> teacherList = teacherService.findAllTeacher();
        model.addAttribute("teacherList",teacherList);
        model.addAttribute("error","");

        return "findAllClazzPage";
    }

    @RequestMapping("/findClazz")
    public String findClazz(Clazz clazz,Model model){
        List<Clazz> clazzList = clazzService.findClazz(clazz);

        if (clazzList.size()<=0){
            model.addAttribute("error","未找到该班级...");

        } else if (clazz.getCno() == null && clazz.getName().equals("") && clazz.getTName().equals("")) {
            model.addAttribute("error","");

        } else {
            model.addAttribute("clazzList",clazzList);
            List<Teacher> teacherList = teacherService.findAllTeacher();
            model.addAttribute("teacherList",teacherList);
            model.addAttribute("error","");

            return "findAllClazzPage";
        }

        return "redirect:/clazz/toFindAllClazzPage/1/5";

    }

}
